#!/usr/bin/env python

import sys
import urllib2
import re
import sqlite3
import os
import allconsuming.filedac

def getAlternateIsbns(isbn):
    data = urllib2.urlopen('http://old-xisbn.oclc.org/webservices/xisbn/' + isbn).read()
    return re.findall('<isbn>([^<]+)</isbn>', data)

def addAllConsumingNumber(isbn, numbers):
    try:
        data = urllib2.urlopen('http://www.allconsuming.net/item/asin/' + isbn).read()
        numbers.append(re.findall('/item/view/([0-9]+)', data)[0])
    except:
        pass

def getIsbn(acn):
    data = urllib2.urlopen('http://www.allconsuming.net/item/view/' + acn).read()
    return re.findall('/asin/([0-9Xx]+)', data)[0]


def extend(connection, allConsumingNumber, isbn=None):
    allAlternates = []
    if isbn is None:
        originalIsbn = getIsbn(allConsumingNumber)
    else:
        originalIsbn = isbn

    for isbn in getAlternateIsbns(originalIsbn):
        print 'found ISBN', isbn
        addAllConsumingNumber(isbn, allAlternates)

    print 'allAlternates =', allAlternates

    c = connection.cursor()
    c.execute('select key from item_map where number = ?', (allConsumingNumber,))
    row = c.fetchone()

    alternatesWeKnowAbout = []
    if not row:
        key = allConsumingNumber
    else:
        key = row[0]
        print 'key =', key
        c.execute('select number from item_map where key = ?', (key,))
        for row in c:
            alternatesWeKnowAbout.append(row[0])
    print 'alternates we know about =', alternatesWeKnowAbout

    for knownAlternate in alternatesWeKnowAbout:
        if knownAlternate in allAlternates:
            allAlternates.remove(knownAlternate)

    print 'unknown alternates = ', allAlternates
    for a in allAlternates:
        print 'adding', key, a
        c.execute('insert into item_map values (?, ?)', (key, a))
    connection.commit()
    

def main(args=None):
    if args == None:
        args = sys.argv[1:]

    conn = sqlite3.connect(os.path.abspath('equivalenceMap.sq3'))

    if len(args) > 1:
        extend(conn, args[0], args[1])
    else:
        extend(conn, args[0])

if __name__ == '__main__':
    sys.exit(main())

